package svgedit.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.List;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MenuListener;

import svgedit.svg.SVGDocument;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGLength;
import svgedit.svg.SVGPaint;
import svgedit.svg.SVGStylable;
import svgedit.svg.SVGViewport;

/** A view onto an {@link SVGDocument}. */
@SuppressWarnings("serial")
public class View extends JComponent implements SVGViewport, ActionListener,MouseListener,
MouseMotionListener{

	private int SAVE_STATE=0;
	private String shapeType="Empty";
	private Frame parentFrame;
	private Point startDragMouse, endDragMouse;
	private Graphics g1;
	private String fillColor ="None";
	private JColorChooser colorChooser;
	private JPopupMenu color;
	private Color selectedColor;
	private String visible="None";
	private Color bgColor=Color.white;
	private Color strokeColor;
	private int strokeWidth=1;
	private Boolean isSelected=false;
	int startX;
	int startY;
	int curX;
	int curY;
	Boolean dragging = false;
	Boolean isButton = false;
	Boolean resizing = false;
	Boolean delete = false;
	Cursor cur = this.getCursor();
	Boolean isInteger;
	Rect rect;
	float dash[] = {10.0f};

	public static ArrayList<Circle> circles = new ArrayList<Circle>();
	public static ArrayList<Line> lines = new ArrayList<Line>();
	public static ArrayList<Rect> rects = new ArrayList<Rect>();
	public static ArrayList<Color> colors = new ArrayList<Color>();

	enum shapeTypes {
		RECTANGLE, LINE, NONE, CIRCLE, SELECT}

	shapeTypes st;

	private SVGDocument document;

	/** Creates a new view */
	public View(Frame frame) {
		parentFrame=frame;
		st=shapeTypes.NONE;
		this.setBackground(Color.black);
		color = new JPopupMenu();
		color.add(new JColorChooser());

		this.addMouseListener(this); 
		//this.addMouseListener(listener);
		//this.addMouseMotionListener(listener);
		this.addMouseMotionListener(this); 


	}

	public float getViewportWidth() {
		return getWidth();
	}

	public float getViewportHeight() {
		return getHeight();
	}

	/** Internal paint entry point.  All drawing in the view uses a {@link Graphics2D},
	 *  so for convenience this method simply delegates to {@link #paint2D}.
	 *
	 *  @param g the {@link Graphics} context to paint to
	 */
	@Override
	protected void paintComponent(Graphics g) {
		paint2D((Graphics2D) g);
		paintShapes((Graphics2D)g);
		//paint2(g);
	}


	public void paintComponent2(Graphics g) {

		paint2(g);
	}




	/** Paints the entire view.
	 *
	 * @param g the {@link Graphics2D} context to paint to
	 */
	private void paint2D(Graphics2D g) {
		// Paint view background

		g.setColor(Color.GRAY);
		g.fillRect(0, 0, getWidth(), getHeight());

		// Paint document background and border
		int documentWidth = (int) document.getWidth().getValue();
		int documentHeight = (int) document.getHeight().getValue();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, documentWidth, documentHeight);
		g.setColor(Color.BLACK);
		g.drawRect(-1, -1, documentWidth, documentHeight);

		// Paint document
		for (SVGElement elem : document) {
			paintElement(g, elem);
		}
	}
	public void paintShapes(Graphics2D g) {
		setSaveState(1);
		for (Color colour : colors) {
			System.out.print("dd");

		}
		for (int i=0; i<colors.size();i++){

			System.out.print(colors);

		}

		for (Circle circle : circles) {
			circle.draw(g);
		}

		for (Rect rect : rects) {
			if (dragging) {

			}
			rect.draw(g);
		}

		for (Line line : lines) {
			line.draw(g);
		}

		if (rects!=null) {
			for (Rect rect : rects) {
				g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				rect.draw(g);
			}
		}

		//define the shape that going to be drawn and draw it as the mouse dragged.
		if (startDragMouse != null && endDragMouse != null){
			if (shapeType.equals("LINE"))
			{
				Line newLine = new Line(startDragMouse.x, startDragMouse.y, endDragMouse.x, 
						endDragMouse.y,bgColor,strokeColor,strokeWidth,isSelected);

				newLine.draw(g);

			}
			if (shapeType.equals("RECTANGLE")) {

				Rect newRectangle = new Rect(startDragMouse.x, startDragMouse.y, endDragMouse.x, 
						endDragMouse.y,bgColor,strokeColor,strokeWidth,isSelected);
				//Shape newCircle = DrawCircle(startDragMouse.x, startDragMouse.y, endDragMouse.x, endDragMouse.y);
				g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);

				newRectangle.draw(g);
			}
			if (shapeType.equals("CIRCLE")) {

				Circle newCirc = new Circle(startDragMouse.x, startDragMouse.y, endDragMouse.x, 
						bgColor,strokeColor,strokeWidth,isSelected);
				//Shape newCircle = DrawCircle(startDragMouse.x, startDragMouse.y, endDragMouse.x, endDragMouse.y);
				g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);

				newCirc.draw(g);
			}

			if (shapeType.equals("SELECT")) {
				if (!dragging) {
					Shape newRect = DrawRectangle(startDragMouse.x, startDragMouse.y, endDragMouse.x, endDragMouse.y);
					g.setColor(Color.black);
					g.setStroke(new BasicStroke(1.0f,
							BasicStroke.CAP_BUTT,
							BasicStroke.JOIN_MITER,
							10.0f, dash, 0.0f));
					g.draw(newRect);
				}
			}

		}

		if (fillColor.equals("None")) {

		}

		// break;
	}




	/** Paints a single element on the graphics context.
	 *
	 * @param g the graphics context to paint to
	 * @param elem the element to paint
	 */
	public void paintElement(Graphics2D g, SVGElement elem) {

		if (!(elem instanceof SVGStylable))
			return;

		Shape shape = elem.createShape();

		SVGStylable style = (SVGStylable) elem;
		SVGPaint fillPaint = style.getFill();
		SVGPaint strokePaint = style.getStroke();
		SVGLength strokeWidth = style.getStrokeWidth();

		// Fill the interior of the shape
		if (fillPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
			g.setPaint(fillPaint.getRGBColor());
			g.fill(shape);
		}

		// Stroke the outline of the shape
		if (strokePaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
			Stroke stroke = new BasicStroke(strokeWidth.getValue());
			g.setStroke(stroke);
			g.setColor(strokePaint.getRGBColor());
			g.draw(shape);
		}
	}

	/** Gets the document currently being displayed by the view. */
	public SVGDocument getDocument() {
		return document;
	}

	/** Sets the document that the view should display.
	 *
	 * @param document the document to set
	 */
	public void setDocument(SVGDocument document) {
		this.document = document;
		repaint();
	}

	public void paint2(Graphics g) {
		Graphics2D g2D = (Graphics2D) g;
		//g2.setStroke(new BasicStroke(2)); //stroke width

	}

	public Ellipse2D.Float DrawCircle(int x1, int y1, int x2,int y2) 
	{


		return new Ellipse2D.Float(x1, y1, x2, y2);	
	}

	public Line2D.Float DrawLine(int x1, int y1, int x2, int y2) 
	{



		return new Line2D.Float(x1,y1,x2,y2);
	}

	public Rectangle2D.Float DrawRectangle(int x1, int y1, int x2, int y2) 
	{



		return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), 
				Math.abs(y1 - y2));
	}

	//Take all the value from the button and pass it to specific command
	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.getActionCommand().toString());
		shapeType = e.getActionCommand().toString();
		visible = e.getActionCommand().toString();
		JButton selected=null;
		JButton selected2=null;
		if (e.getActionCommand().toString()=="insertRect") {
			shapeType="RECTANGLE";
			st=shapeTypes.RECTANGLE;
			st=st.RECTANGLE;
			isSelected = false;
			dragging=false;
			cleanSelected();
			this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		}
		if (e.getActionCommand().toString()=="insertCircle"){
			shapeType="CIRCLE";
			st=shapeTypes.CIRCLE;
			isSelected = false;
			dragging=false;
			cleanSelected();
			this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		}
		if (e.getActionCommand().toString()=="insertLine") {
			st=shapeTypes.LINE;
			st=st.LINE;
			shapeType="LINE";
			isSelected = false;
			dragging=false;
			cleanSelected();
			this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		}

		if (e.getActionCommand().toString()!="insertRect" || e.getActionCommand().toString()!="insertCircle" ||
				e.getActionCommand().toString()!="insertLine") {
				
				selected  = (JButton)e.getSource();
				selected2  = (JButton)e.getSource();
		}
		if (selected == parentFrame.getButtons().get(Frame.RECTANGLE)) {
			shapeType="RECTANGLE";
			st=shapeTypes.RECTANGLE;
			st=st.RECTANGLE;
			isSelected = false;
			dragging=false;
			cleanSelected();
			this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		}
		if (selected == parentFrame.getButtons().get(Frame.CIRCLE)) {
			shapeType="CIRCLE";
			st=shapeTypes.CIRCLE;
			isSelected = false;
			dragging=false;
			cleanSelected();
			this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		}
		if (selected == parentFrame.getButtons().get(Frame.LINE)) {
			st=shapeTypes.LINE;
			st=st.LINE;
			shapeType="LINE";
			isSelected = false;
			dragging=false;
			cleanSelected();
			this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		}
		if (selected == null) {
			st=shapeTypes.NONE;
			dragging=false;
			st=st.NONE;
			cleanSelected();
			this.setCursor(cur);
		}
		if (selected == parentFrame.getButtons().get(Frame.SELECT)) {
			isButton = true;
			shapeType="SELECT";
			st=shapeTypes.SELECT;
			st=st.SELECT;
			dragging=false;
			this.setCursor(cur);
		}
		if (selected2 == parentFrame.getButtons2().get(Frame.FILL)) {
			bgColor
			= JColorChooser.showDialog(this,
					"Choose Background Color",
					Color.white);

		}
		if (selected == parentFrame.getButtons2().get(Frame.STROKE_COLOR)) {
			strokeColor
			= JColorChooser.showDialog(this,
					"Choose Background Color",
					Color.white);
		}
		if (selected == parentFrame.getButtons2().get(Frame.STROKE_WIDTH)) {

			String strokeWidths
			= JOptionPane.showInputDialog(this,
			"Please Enter StrokeWidth");
			if (strokeWidths==null || strokeWidths.equals("")) {

			}


			else if (isInteger = Pattern.matches("^\\d*$", strokeWidths)) {
				if (Integer.parseInt(strokeWidths)<0) {	
				}
				else {
					strokeWidth = Integer.parseInt(strokeWidths);
				}
			}
			else {

			}
		}

		if (selected == parentFrame.getButtons2().get(Frame.delete)) {
			//delete = true;
			if(rects!=null) {

				Iterator<Rect> rectIterator = rects.iterator();

				while (rectIterator.hasNext()) {
					if (rectIterator.next().getIsSelected()==true) {
						rectIterator.remove();
						repaint();
						this.setCursor(cur);
					}
				}
			}

			if (circles!=null) {
				Iterator<Circle> circIterator = circles.iterator();

				while (circIterator.hasNext()) {
					if (circIterator.next().getIsSelected()==true) {
						circIterator.remove();
						repaint();
						this.setCursor(cur);	
					}
				}
			}

			if (lines!=null) {
				Iterator<Line> lineIterator = lines.iterator();

				while (lineIterator.hasNext()) {
					if (lineIterator.next().getIsSelected()==true) {
						lineIterator.remove();
						repaint();
						this.setCursor(cur);
					}
				}
			}
			
		}

		if (selected == parentFrame.getButtons2().get(Frame.noFill)) {
			bgColor=new Color(255,255,255,100);

		}

		if (selected == parentFrame.getButtons2().get(Frame.noStroke)) {
			strokeColor=new Color(255,255,255);
			for (Rect rect : rects) {
				System.out.print(rect.getStrokeColor().toString().substring(23, 26));
			}
		}
		
	}



	public void colorPick2() {
		color.getSelectionModel().addChangeListener(
				new ChangeListener() {
					public void stateChanged(ChangeEvent e) {
						colorChooser.setColor(Color.black);
						Color newColor = colorChooser.getColor();
					}
				});
	}


	public String getShapeType() {
		return shapeType;
	}

	public void setShapeType(String shapeType) {
		this.shapeType = shapeType;
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		if(resizing) {
			Point p = e.getPoint();
			for (Rect rect : rects){
				if(rect.getIsSelected()==true){ 



					int type = this.getCursor().getType();
					int dx = p.x - rect.x1;
					int dy = p.y - rect.y1;
					switch(type) {


					case Cursor.N_RESIZE_CURSOR:
						int height = rect.getHeight() - dy;
						rect.x1=rect.x1;
						rect.x2=rect.x2+dy;
						rect.width=rect.width;
						rect.height=rect.height;
						repaint();
						break;
						//	                case Cursor.NW_RESIZE_CURSOR:
						//	                    int width = r.width - dx;
						//	                    height = r.height - dy;
						//	                    r.setRect(r.x+dx, r.y+dy, width, height);
						//	                    break;
						//	                case Cursor.W_RESIZE_CURSOR:
						//	                    width = r.width - dx;
						//	                    r.setRect(r.x+dx, r.y, width, r.height);
						//	                    l.setLine(l.getP1(), l.getP2());
						//	                    break;
						//	                case Cursor.SW_RESIZE_CURSOR:
						//	                    width = r.width - dx;
						//	                    height = dy;
						//	                    r.setRect(r.x+dx, r.y, width, height);
						//	                    break;
						//	                case Cursor.S_RESIZE_CURSOR:
						//	                    height = dy;
						//	                    r.setRect(r.x, r.y, r.width, height);
						//	                    break;
						//	                case Cursor.SE_RESIZE_CURSOR:
						//	                    width = dx;
						//	                    height = dy;
						//	                    r.setRect(r.x, r.y, width, height);
						//	                    break;
						//	                case Cursor.E_RESIZE_CURSOR:
						//	                    width = dx;
						//	                    r.setRect(r.x, r.y, width, r.height);
						//	                    l.setLine(l.getP1(), l.getP2());
						//	                    break;
						//	                case Cursor.NE_RESIZE_CURSOR:
						//	                    width = dx;
						//	                    height = r.height - dy;
						//	                    r.setRect(r.x, r.y+dy, width, height);
						//	                    break;
						//	               

					default:
						System.out.println("unexpected type: " + type);
					}
					repaint();
				}
			}
		}
		switch(st) {

		case RECTANGLE: {
			endDragMouse = new Point(e.getX(), e.getY());
			repaint();
			break;
		}

		case LINE: {
			endDragMouse = new Point(e.getX(), e.getY());
			repaint();
			break;
		}
		case CIRCLE: {
			endDragMouse = new Point(e.getX(), e.getY());
			repaint();
			break;
		}
		case NONE: {

			break;
		}

		case SELECT: {
			endDragMouse = new Point(e.getX(), e.getY());
			repaint();
			break;
		}

		}

		if(isButton==true){
			for(Rect rect : rects) {
				if(rect.getIsSelected()==true) {
					if(dragging){
						int width=rect.getWidth();
						int height = rect.getHeight();
						rect.x1=e.getX();
						rect.y1=e.getY();
						rect.x2 = rect.x1+width;
						rect.y2 = rect.y1+height;
						rect.color=bgColor;
						repaint();
					}
				}
			}
			for(Circle circle : circles) {
				if(circle.isSelected==true) {
					if(dragging){
						int radius = circle.getRadius();

						circle.x1=e.getX();
						circle.y1=e.getY();
						circle.radius = radius;
						circle.color=bgColor;
						repaint();
					}
				}
			}
			for(Rect rect : rects) {
				if(rect.getIsSelected()==true) {
					if(dragging){
						int width=rect.getWidth();
						int height = rect.getHeight();
						rect.x1=e.getX();
						rect.y1=e.getY();
						rect.x2 = rect.x1+width;
						rect.y2 = rect.y1+height;
						rect.color=bgColor;
						repaint();
					}
				}
			}
			for(Line line : lines) {
				if(line.isSelected==true) {
					if(dragging){
						int dx = line.getDX();
						int dy = line.getDY();
						line.x1=e.getX();
						line.y1=e.getY();
						line.x2 = line.x1+dx;
						line.y2 = line.y1+dy;
						line.color=bgColor;
						repaint();
					}
				}
			}
		}
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		Point p = e.getPoint();
		if(!circles.isEmpty()) {
			for(Circle circle : circles) {
				if (Math.pow((circle.getX() - e.getX()),2) + Math.pow((circle.getY() - e.getY()),2) < 
						Math.pow((circle.getRadius()),2))
					circle.isSelected=true;
				else {
					circle.isSelected=false;
					dragging = false;
				}
			}
			for(Rect rect : rects) {
				int delta_x=e.getX()-rect.x1;
				int delta_y=e.getY()-rect.y1;

				if(delta_x>0 && delta_x<=rect.getWidth() && delta_y>0
						&&delta_y<=rect.getHeight())
					rect.setIsSelected(true);		
				else {
					rect.setIsSelected(false);
					dragging = false;
				}

			}
			for (Line line : lines) {
				double i = line.y2-line.y1;
				double j = line.x2-line.x1;
				double m = i/j;
				double c= line.y1-(m*line.x1);
				if (e.getY()== (m*e.getX())+c)
					line.isSelected=true;
				else {
					line.isSelected=false;
					dragging = false;
				}
			}
		}
	}	

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {

		if (isButton == true) {
			for (Rect rect : rects) {
				if (rect.getIsSelected()==true){
					startX = e.getX();
					startY = e.getY();
					dragging = true; 
				}
			}

			if (isButton ==  true) {
				for (Rect rect : rects) {
					if (rect.getIsSelected()==true){
						if(this.getCursor() != Cursor.getDefaultCursor()) {
							resizing = true;
						}
					}
				}
			}
			for (Circle circle : circles) {
				if (circle.isSelected==true){
					startX = e.getX();
					startY = e.getY();
					dragging = true; 
				}
			}

			if (isButton ==  true) {
				for (Circle circle : circles) {
					if (circle.isSelected==true){
						if(this.getCursor() != Cursor.getDefaultCursor()) {
							resizing = true;
						}
					}
				}
			}

		}

		switch(st) {

		case RECTANGLE: {
			startDragMouse = new Point(e.getX(), e.getY());
			endDragMouse = startDragMouse;
			repaint();
			break;
		}

		case LINE: {
			startDragMouse = new Point(e.getX(), e.getY());
			endDragMouse = startDragMouse;
			repaint();
			break;
		}

		case CIRCLE: {
			startDragMouse = new Point(e.getX(), e.getY());
			endDragMouse = startDragMouse;
			repaint();
			break;
		}
		case NONE: {

			break;
		}

		case SELECT: {
			startDragMouse = new Point(e.getX(), e.getY());
			endDragMouse = startDragMouse;
			repaint();
			break;
		}

		}
	}


	public void mouseReleased(MouseEvent e) {
		resizing = false;

		switch(st) {
		case RECTANGLE: {
			Rect newRect = new Rect(startDragMouse.x, startDragMouse.y, e.getX(), 
					e.getY(),bgColor,strokeColor,strokeWidth,isSelected);

			rects.add(newRect);
			startDragMouse = null;
			endDragMouse = null;
			repaint();
			break;
		}

		case LINE: {
			Line newLine = new Line(startDragMouse.x, startDragMouse.y, e.getX(), 
					e.getY(),bgColor,strokeColor,strokeWidth,isSelected);

			lines.add(newLine);
			startDragMouse = null;
			endDragMouse = null;
			repaint();
			break;
		}

		case CIRCLE: {
			Circle newCirc = new Circle(startDragMouse.x, startDragMouse.y, e.getX(), 
					bgColor,strokeColor,strokeWidth,isSelected);
			circles.add(newCirc);
			startDragMouse = null;
			endDragMouse = null;
			repaint();
			break;

		}
		case NONE: {
			break;
		}

		case SELECT: {
			//SELECT THE OBJECT
			Rectangle r = new Rectangle(startDragMouse.x, startDragMouse.y, e.getX(), 
					e.getY());

			if(!circles.isEmpty()) {
				for(Circle circle : circles) {
					if (r.intersects(circle.x1, circle.y1, circle.radius, circle.radius))
						circle.isSelected=true;
					else {
						circle.isSelected=false;
						dragging = false;
					}
				}

			}
			if(!rects.isEmpty()) {
				for(Rect rect : rects) {
					if(r.intersects(rect.x1, rect.y1, rect.x2, rect.y2))
						rect.setIsSelected(true);
					else {
						rect.setIsSelected(false);
						dragging = false;
					}
				}
			}
			if(!lines.isEmpty()) {
				for(Line line : lines) {
					if(r.intersects(line.x1, line.y1, line.x2, line.y2))
						line.isSelected=true;
					else {
						line.isSelected=false;
						dragging = false;
					}

				}
			}	
			startDragMouse = null;
			endDragMouse = null;
			repaint();
		}
		}

	}
	public int getSaveState()
	{
		return SAVE_STATE;
	}
	public void setSaveState(int state)
	{
		System.out.println("SAVE_STATE= "+state);
		SAVE_STATE=state;
	}
	public shapeTypes getSt() {
		return st;
	}

	public void setSt(shapeTypes st) {
		this.st = st;
	}

	public static ArrayList<Circle> getCircles() {
		return circles;
	}
	public static ArrayList<Line> getLines() {
		return lines;
	}
	public static ArrayList<Rect> getRects() {
		return rects;
	}
	public static ArrayList<Color> getColors() {
		return colors;
	}
	
	public void cleanSelected(){

		if(!circles.isEmpty()) {
			for(Circle circle : circles) {
				circle.isSelected=false;
				dragging = false;
			}
		}

		if(!lines.isEmpty()) {
			for(Line line : lines) {
				line.isSelected=false;
				dragging = false;
			}
		}
		if(!rects.isEmpty()) {
			for(Rect rect : rects) {
				rect.setIsSelected(false);
				dragging = false;
			}
		}
	}
}

